package com.feiyi.app.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.feiyi.app.R;
import com.feiyi.app.activity.ArticleDetailActivity;
import com.feiyi.app.activity.GoodsDetailActivity;
import com.feiyi.app.activity.GoodsDetailActivityNew;
import com.feiyi.app.activity.GoodsSearchActivity;
import com.feiyi.app.app.AppApplication;

import com.feiyi.app.bean.HomePageBean;
import com.feiyi.app.glideutils.GlideImgManager;


import java.util.List;

/**
 * 图片浏览的PagerAdapter
 * 
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {

	private Context context;
	List<HomePageBean.DataBean.AdsBean> list;
	private AppApplication application;
	public ImagePagerAdapter(Context context, List<HomePageBean.DataBean.AdsBean> list) {
		this.context = context;
		this.list = list;
		application = AppApplication.getApp();

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(final int position, View convertView, ViewGroup container) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			ImageView imageView = new ImageView(context);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			convertView = holder.imageView = imageView;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final HomePageBean.DataBean.AdsBean item = list.get(position);
		if (null != item) {

			if (item.getSrc().isEmpty() || item.getSrc() == null) {
				holder.imageView.setBackgroundResource(R.mipmap.ic_launcher);
			} else {
				GlideImgManager.loadImage(context,  item.getSrc(),holder.imageView);
			}

			convertView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (null != item.getModel()){
						Bundle bundle = new Bundle();
						switch (item.getModel()){
							case "article":
								bundle.putString("article_id",item.getModel_id());
								ActivityUtils.startActivity(bundle, ArticleDetailActivity.class);
								break;
							case "goods":
								bundle.putString("goods_id",item.getModel_id());
								ActivityUtils.startActivity(bundle, GoodsDetailActivityNew.class);
								break;
							case "category":
								bundle.putString("category_id",item.getModel_id());
								ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
								break;
							case "brand":
								bundle.putString("brand_id",item.getModel_id());
								ActivityUtils.startActivity(bundle, GoodsSearchActivity.class);
								break;

						}
					}

				}
			});
		}

		return convertView;
	}

	private static class ViewHolder {
		ImageView imageView;
	}

}
